﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows;
using System.Collections;
using System.Data;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Soket_MMTNC_Client
{
    class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public string Price { get; set; }
        public string Quantity { get; set; }
        public string UnitPrice { get; set; }

        public List<Product> GetData()
        {
            XDocument doc = XDocument.Load("../../databases/products.xml");
            if (doc == null) MessageBox.Show("doc is null");
            var product = from ele in doc.Descendants("product")
                          select new
                          {
                              ProductID = ele.Element("id").Value,
                              ProductName = ele.Element("name").Value,
                              Price = ele.Element("price").Value,
                              Quantity = ele.Element("quantity").Value,
                              UnitPrice = ele.Element("unit").Value,

                          };
            List<Product> p = new List<Product>();
            foreach (var pr in product)
            {
                p.Add(new Product()
                {
                    ProductID = int.Parse(pr.ProductID),
                    ProductName = pr.ProductName,
                    Price = pr.Price,
                    Quantity = pr.Quantity,
                    UnitPrice = pr.UnitPrice
                });
            }

            return p;
        }

        public Product GetItem(int ProductID)
        {
            XDocument doc = XDocument.Load("../../databases/products.xml");
            if (doc == null) MessageBox.Show("doc is null");
            var product = from ele in doc.Descendants("product")
                          where ele.Element("id").Value == ProductID.ToString()
                          select new
                          {
                              ProductID = ele.Element("id").Value,
                              ProductName = ele.Element("name").Value,
                              Price = ele.Element("price").Value,
                              UnitPrice = ele.Element("unit").Value,

                          };
            Product p = new Product();
            foreach (var pr in product)
            {
                p.ProductID = int.Parse(pr.ProductID);
                p.ProductName = pr.ProductName;
                p.Price = pr.Price;
                p.UnitPrice = pr.UnitPrice;
            }

            return p;
        }

        public void AddBill(int ProductID,string Quantity)
        {
            
            XDocument doc = XDocument.Load("../../databases/bills.xml");

            doc.Element("bills").Add(new XElement("bill",
                new XElement("id", ProductID), new XElement("quantity", Quantity),
                new XElement("date", DateTime.Today.ToString("M/dd/yyyy"))));
            doc.Save("../../databases/bills.xml");
        }

        public void UpdateQuantity(int ProductID, string Quantity)
        {
            XDocument doc = XDocument.Load("../../databases/products.xml");

            IEnumerable<System.Xml.Linq.XElement> product = from p in doc.Descendants("product")
                                                            where p.Element("id").Value == ProductID.ToString()
                                                            select p;
            if (product.Count() > 0)
            {
                foreach (XElement p in product)
                {
                    p.SetElementValue("quantity",int.Parse(p.Element("quantity").Value)-int.Parse(Quantity));
                }
                doc.Save("../../databases/products.xml");
            }
        }

        public void UpdatePrice(string id, string Price)
        {
            XDocument doc = XDocument.Load("../../databases/products.xml");

            IEnumerable<System.Xml.Linq.XElement> product = from p in doc.Descendants("product")
                                                            where p.Element("id").Value ==id
                                                            select p;
            if (product.Count() > 0)
            {
                foreach (XElement p in product)
                {
                    p.SetElementValue("price",Price);
                }
                doc.Save("../../databases/products.xml");
            }
        }

        public DataTable Statistics(string Date)
        {
            XDocument doc = XDocument.Load("../../databases/bills.xml");
            if (doc == null) MessageBox.Show("doc is null");                    
            
            DataTable dt = new DataTable();

            XElement setup = (from p in doc.Descendants("bill") select p).First();
            foreach (XElement xe in setup.Descendants()) // build your DataTable
                dt.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt

            var all = from ele in doc.Descendants("bill")
                      where ele.Element("date").Value == Date
                      select ele;
            foreach (XElement xe in all)
            {
                DataRow dr = dt.NewRow();
                foreach (XElement xe2 in xe.Descendants())
                    dr[xe2.Name.ToString()] = xe2.Value; //add in the values
                dt.Rows.Add(dr);
            }
            return dt;
        }

        public string IDMax(string Date)
        {
            XDocument doc = XDocument.Load("../../databases/bills.xml");
            if (doc == null) MessageBox.Show("doc is null");
            var GroupBill = from ele in doc.Descendants("bill")
                            where ele.Element("date").Value == Date
                            group ele by ele.Element("id").Value into g
                            select new
                            {
                                ID = g.Key,
                                Quantity = g.Sum(c => (decimal)c.Element("quantity"))
                            };            
            string id = "";
            int q=0;
            int q1;
            foreach (var gp in GroupBill)
            {
                if (id == "")
                {
                    id = gp.ID.ToString();
                    q=(int)gp.Quantity;
                }
                else
                {
                    q1=(int)gp.Quantity;
                    if ( q1> q)
                    {
                        id = gp.ID.ToString();
                        q = q1;
                    }
                }
            }

            return id.ToString();
        }

        public string IDMin(string Date)
        {
            XDocument doc = XDocument.Load("../../databases/bills.xml");
            if (doc == null) MessageBox.Show("doc is null");
            var GroupBill = from ele in doc.Descendants("bill")
                            where ele.Element("date").Value == Date
                            group ele by ele.Element("id").Value into g
                            select new
                            {
                                ID = g.Key,
                                Quantity = g.Sum(c => (decimal)c.Element("quantity"))
                            };
            string id = "";
            int q = 0;
            int q1;
            foreach (var gp in GroupBill)
            {
                if (id == "")
                {
                    id = gp.ID.ToString();
                    q = (int)gp.Quantity;
                }
                else
                {
                    q1 = (int)gp.Quantity;
                    if (q1< q)
                    {
                        id = gp.ID.ToString();
                        q = q1;
                    }
                }
            }

            return id.ToString();
        }

        public DataTable Statistics(string Date, string ID)
        {
            XDocument doc = XDocument.Load("../../databases/bills.xml");
            if (doc == null) MessageBox.Show("doc is null");

            DataTable dt = new DataTable();

            XElement setup = (from p in doc.Descendants("bill") select p).First();
            foreach (XElement xe in setup.Descendants()) // build your DataTable
                dt.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt

            var all = from ele in doc.Descendants("bill")
                      where ele.Element("date").Value == Date && ele.Element("id").Value == ID
                      select ele;
            foreach (XElement xe in all)
            {
                DataRow dr = dt.NewRow();
                foreach (XElement xe2 in xe.Descendants())
                    dr[xe2.Name.ToString()] = xe2.Value; //add in the values
                dt.Rows.Add(dr);
            }
            return dt;
        }

      
   
    }
}
